package ddb.communication;

import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

import ddb.dbcore.*;

public class ClientBase {
    public static void main(String args[]) throws IOException {
        HttpClient httpclient = new DefaultHttpClient();
        HttpGet httpget = new HttpGet("http://localhost:8000/applications/myapp");
        HttpResponse response = httpclient.execute(httpget);
        HttpEntity entity = response.getEntity();
        if (entity != null) {
            InputStream instream = entity.getContent();
            ObjectInputStream ois = new ObjectInputStream(instream);
            try {
                DbTable table = (DbTable)ois.readObject();
                for (int i = 0; i < table.getRowCount(); i ++) {
                    int numColumns = table.rowAt(i).size();
                    for ( int j = 0; j < numColumns ; j++ ) {
                       // Column numbers start at 1.
                       // Also there are many methods on the result set to return
                       //  the column as a particular type. Refer to the Sun documentation
                       //  for the list of valid conversions.
                       System.out.println( "COLUMN " + j + " = " + table.rowAt(i).get(j) );
                    }
                }
            }
            catch (Exception ex) {
                System.out.println(ex.toString());
            }
            //BufferedReader bufReader = new BufferedReader(new InputStreamReader(instream));
            //System.out.println(bufReader.readLine());
        }
    }
}
